﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using phudieuhungthinh.BL.Model;
using phudieuhungthinh.DAL.Repositories;

namespace phudieuhungthinh.BL
{
    public class ArticleImpl
    {

        #region Get
        public ArticleModel GetArticle(int id)
        {
            try
            {
                var articleRepository = new ArticleRepository();
                var article = new ArticleModel(articleRepository.GetSingleArticle(x => x.ID == id));
                return article;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public ArticleModel GetArticle(string url)
        {
            try
            {
                var articleRepository = new ArticleRepository();
                var article = new ArticleModel(articleRepository.GetSingleArticle(x => x.Url == url));
                return article;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public ArticleModel GetArticle(ArticleType type)
        {
            try
            {
                var articleRepository = new ArticleRepository();
                var article = new ArticleModel(articleRepository.GetSingleArticle(x => x.ArticleType == (int)type && x.IsPublished));
                return article;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<ArticleModel> GetArticles(ArticleType type)
        {
            try
            {
                var articleRepository = new ArticleRepository();
                var articles = (from x in articleRepository.QueryArticle(x => x.ArticleType == (int)type)
                               select x).ToList().Select(x => new ArticleModel(x) { ArticleSapo = string.Empty, ArticleContent = string.Empty}).ToList();
                return articles;
            }
            catch (Exception)
            {
                return new List<ArticleModel>();
            }
        }

        public List<ArticleModel> GetArticles(ArticleType type, bool isPublished)
        {
            try
            {
                var articleRepository = new ArticleRepository();
                var article = (from x in articleRepository.QueryArticle(x => x.ArticleType == (int)type)
                               where x.IsPublished == isPublished
                               select new ArticleModel(x)).ToList();
                return article;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<ArticleModel> GetArticlesExceptFeature(ArticleType type, bool isPublished)
        {
            try
            {
                var articleRep = new ArticleRepository();
                var featureArticleRep = new FeatureArticleRepository();
                var features = featureArticleRep.GetAllFeatureArticle();
                var article = (from x in articleRep.QueryArticle(x => x.ArticleType == (int)type)
                               where x.IsPublished == isPublished
                               && features.All(y => y.ArticleID != x.ID)
                               select x).ToList().Select(x => new ArticleModel(x)).ToList();
                return article;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public string GetThumb(string url)
        {
            var articleRep = new ArticleRepository();
            return articleRep.GetSingleArticle(x => x.Url == url).Thumb;
        }

        #endregion

        #region Insert
        public void SaveArticle(ArticleModel articleModel)
        {
            try
            {
                var articleRepository = new ArticleRepository();
                if (!string.IsNullOrEmpty(articleModel.Thumb) && articleModel.Thumb.StartsWith("http://"))
                {
                    string fileName = Path.GetFileName(articleModel.Thumb);
                    articleModel.Thumb = fileName;
                }
                if (articleModel.ID != 0)
                {
                    if (!string.IsNullOrEmpty(articleModel.Title))
                        articleModel.Url = StringUtil.RemoveSign4VietnameseString(articleModel.Title.ToLower()).Replace(' ', '-');
                    
                    var art = articleRepository.GetSingleArticle(x => x.ID == articleModel.ID);

                    art.Thumb = articleModel.Thumb;
                    art.Title = articleModel.Title;
                    art.Url = articleModel.Url;
                    art.IsPublished = true;
                    art.ArticleType = articleModel.ArticleType;
                    art.ArticleSapo = articleModel.ArticleSapo;
                    art.ArticleContent = articleModel.ArticleContent;
                    articleRepository.UpdateArticle(art);
                }
                else
                {
                    articleModel.Url = StringUtil.RemoveSign4VietnameseString(articleModel.Title.ToLower()).Replace(' ', '-');
                    articleRepository.AddArticle(articleModel.MapArticle());
                }
                articleRepository.Commit();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region Remove
        public bool DeleteArticle(int id)
        {
            try
            {
                var articleRep = new ArticleRepository();
                var article = articleRep.GetSingleArticle(x => x.ID == id);
                if (article == null) return false;
                articleRep.DeleteArticle(article);
                articleRep.Commit();

                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
        
    }
}